package com.yundasys.lab12java.concurrent;

import java.util.concurrent.CompletableFuture;

/**
 * @PACKAGE_NAME: com.yundasys.lab12java.concurrent
 * @NAME: CompletableFutureTest1
 * @USER: jiangta
 * @DATE: 2022/11/4
 * @TIME: 8:59 上午
 * @PROJECT_NAME: springboot-labs
 */
public class CompletableFutureTest1 {

    public static void main(String[] args) throws InterruptedException {
        CompletableFuture<String> cfquery = CompletableFuture.supplyAsync(() -> {
            return queryCode("中国石油");
        });
        CompletableFuture<Double> doubleCompletableFuture = cfquery.thenApplyAsync((code) -> {
            return fetchPrice(code);
        });
        doubleCompletableFuture.thenAccept((result)->{
            System.out.println(result);
        });

        // 主线程不要立刻结束，否则CompletableFuture默认使用的线程池会立刻关闭:
        Thread.sleep(2000);
    }


    static String queryCode(String name) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        return "601857";
    }

    static Double fetchPrice(String code) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        return 5 + Math.random() * 20;
    }
}
